package thread.matrix.individual;

/**
 * 矩阵中每个元素一个线程：细粒度
 * 任务类：计算结果矩阵某个元素的值
 */
public class IndividualMultiplierTask implements Runnable{
    //加final保证可见性，避免线程冲突
    private final double[][] matrix1;
    private final double[][] matrix2;
    private final double[][] result;
    private final int i;//matrix1的当前行数
    private final int j;//matrix2的当前列数
    public IndividualMultiplierTask(double[][] matrix1, double[][] matrix2, double[][] result, int i, int j) {
        this.matrix1 = matrix1;
        this.matrix2 = matrix2;
        this.result = result;
        this.i = i;
        this.j = j;
    }



    @Override
    public void run() {
        //计算结果矩阵某个元素的值
        //为result矩阵赋初值
        result[i][j] = 0;
        for(int k=0;k<matrix1[i].length;k++)//矩阵1的列数
            result[i][j] += matrix1[i][k]*matrix2[k][j];
    }
}
